﻿using System;
using System.Linq;
using System.Web.Mvc;
using MvcContrib.Sorting;
using MvcContrib.UI.Grid;
using QDT.Common;
using QDT.Common.Data;
using QDT.Core.Members;
using QDT.MVC.Security;

namespace QDT.Web.Admin.Controllers {
    [SecurityModule(Name = "会员配置")]
    public class UserGroupController : BaseController {

        [SecurityNode(Name = "会员组别列表")]
        public ActionResult Index(int? page, GridSortOptions sort) {
            const int pageSize = 10;
            int pageNumber = page ?? 1;

            if (sort.Column == null) sort.Column = "ID";

            ViewData["sort"] = sort;
            IQueryable<UserGroup> source = DbProvider.D<UserGroup>().OrderBy(sort.Column,
                                                                             sort.Direction == SortDirection.Ascending);

            return View(source.ToPagedList(pageNumber - 1, pageSize));
        }

        [SecurityNode(Name = "添加会员组页面")]
        public ActionResult Add() {
            return View();
        }

        [SecurityNode(Name = "添加会员组操作")]
        [HttpPost]
        public ActionResult PostAdd(string name, int? score) {
            if (string.IsNullOrWhiteSpace(name)) return Error("会员组名称是必填的");

            if (DbProvider.D<UserGroup>().Count(x => x.Name == name) > 0)

                throw new ApplicationException("会员组名称已经存在,不能出现相同的组名");

            var group = new UserGroup {
                                          Name = name,
                                          Score = score ?? 0
                                      };

            DbProvider.Add(group);
            DbProvider.SaveChanges();

            return Success("操作成功");
        }

        [SecurityNode(Name = "编辑会员组页面")]
        public ActionResult Edit(int id) {
            var group = DbProvider.GetByID<UserGroup>(id);

            return View(group);
        }

        [SecurityNode(Name = "编辑会员组执行")]
        [HttpPost]
        public ActionResult PostEdit(int id, string name, int? score) {
            if (string.IsNullOrWhiteSpace(name))

                return Error("会员组名称是必填的");

            if (DbProvider.D<UserGroup>().Count(x => x.Name == name && x.ID != id) > 0)

                throw new ApplicationException("会员组名称已经存在,不能出现相同的组名");

            var group = DbProvider.GetByID<UserGroup>(id);

            if (group == null) return Error("会员组不存在");

            group.Name = name;
            group.Score = score ?? 0;

            DbProvider.SaveChanges();

            return Success("操作成功");
        }

        [SecurityNode(Name = "永久删除")]
        [HttpPost]
        public ActionResult ForeverDel(int[] id) {

            if (id == null || id.Count() == 0) return Error("未选择会员组");

            foreach (var i in id)
            {
                var group = DbProvider.GetByID<UserGroup>(i);

                if (group == null) continue;

                if (DbProvider.D<User>().Count(x => x.GroupID == i) > 0) throw new ApplicationException("{0} 会员组下存在会员".FormatWith(group.Name));

                DbProvider.Delete(group);
            }

            DbProvider.SaveChanges();

            return Success("操作成功");
        }
    }
}